package com.ruoyi.web.controller.mbti;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MbtiTestType;
import com.ruoyi.system.service.IMbtiTestTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * MBTI测试类型Controller
 * 
 * @author ruoyi
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/mbti/testType")
public class MbtiTestTypeController extends BaseController
{
    @Autowired
    private IMbtiTestTypeService mbtiTestTypeService;

    /**
     * 查询MBTI测试类型列表
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:list')")
    @GetMapping("/list")
    public TableDataInfo list(MbtiTestType mbtiTestType)
    {
        startPage();
        List<MbtiTestType> list = mbtiTestTypeService.selectMbtiTestTypeList(mbtiTestType);
        return getDataTable(list);
    }

    /**
     * 导出MBTI测试类型列表
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:export')")
    @Log(title = "MBTI测试类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MbtiTestType mbtiTestType)
    {
        List<MbtiTestType> list = mbtiTestTypeService.selectMbtiTestTypeList(mbtiTestType);
        ExcelUtil<MbtiTestType> util = new ExcelUtil<MbtiTestType>(MbtiTestType.class);
        util.exportExcel(response, list, "MBTI测试类型数据");
    }

    /**
     * 获取MBTI测试类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:query')")
    @GetMapping(value = "/{testTypeId}")
    public AjaxResult getInfo(@PathVariable("testTypeId") Long testTypeId)
    {
        return success(mbtiTestTypeService.selectMbtiTestTypeByTestTypeId(testTypeId));
    }

    /**
     * 新增MBTI测试类型
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:add')")
    @Log(title = "MBTI测试类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MbtiTestType mbtiTestType)
    {
        if (!mbtiTestTypeService.checkTypeCodeUnique(mbtiTestType))
        {
            return error("新增测试类型'" + mbtiTestType.getTypeName() + "'失败，类型编码已存在");
        }
        mbtiTestType.setCreateBy(getUsername());
        return toAjax(mbtiTestTypeService.insertMbtiTestType(mbtiTestType));
    }

    /**
     * 修改MBTI测试类型
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:edit')")
    @Log(title = "MBTI测试类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MbtiTestType mbtiTestType)
    {
        if (!mbtiTestTypeService.checkTypeCodeUnique(mbtiTestType))
        {
            return error("修改测试类型'" + mbtiTestType.getTypeName() + "'失败，类型编码已存在");
        }
        mbtiTestType.setUpdateBy(getUsername());
        return toAjax(mbtiTestTypeService.updateMbtiTestType(mbtiTestType));
    }

    /**
     * 删除MBTI测试类型
     */
    @PreAuthorize("@ss.hasPermi('mbti:testType:remove')")
    @Log(title = "MBTI测试类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{testTypeIds}")
    public AjaxResult remove(@PathVariable Long[] testTypeIds)
    {
        return toAjax(mbtiTestTypeService.deleteMbtiTestTypeByTestTypeIds(testTypeIds));
    }

    /**
     * 校验类型编码
     */
    @PostMapping("/checkTypeCodeUnique")
    public AjaxResult checkTypeCodeUnique(@RequestBody MbtiTestType mbtiTestType)
    {
        return AjaxResult.success(mbtiTestTypeService.checkTypeCodeUnique(mbtiTestType));
    }
}
